<template>
  <div class="register">
    <div class="log"></div>
    <van-form @submit="onSubmit">
      <van-field v-model="username" name="name" label="用户名" placeholder="用户名" :rules="[{ required: true, message: '请填写用户名' }]" />
      <van-field v-model="password" type="password" name="pwd" label="密码" placeholder="密码" :rules="[{ required: true, message: '请填写密码' }]" />
      <div style="margin: 16px">
        <van-button round block type="info" native-type="submit">注册</van-button>
      </div>
      <div class="Jump">
        <a href="#/home"></a>
        <a href="#/login">去登入</a>
      </div>
    </van-form>
  </div>
</template>
<script>
import { Toast } from 'vant'
export default {
  created() {
    this.localData = JSON.parse(localStorage.getItem('userData') || '[]')
  },
  data() {
    return {
      username: '',
      password: '',
      localData: [],
      isReg: false
    }
  },
  methods: {
    onSubmit(values) {
      for (let i = 0; i < this.localData.length; i++) {
        if (this.localData[i].name === this.username) {
          Toast('该用户已注册!')
          this.isReg = true
          break
        }
      }
      if (this.isReg === false && values) {
        this.localData.push(values)
        localStorage.setItem('userData', JSON.stringify(this.localData))
        Toast('注册成功!')
      }
    }
  }
}
</script>

<style lang="scss" scoped>
// .log {
//   width: 100%;
//   height: 77px;
//   background-size: 140px 40px;
//   margin-bottom: 20px;
// }
.Jump {
  display: flex;
  justify-content: space-between;
  padding: 0px 25px;
  text-decoration: underline;
}
</style>
